package bug
{
	public class Wisdom
	{
		private var _scene:Scene;
		private var _power:int=PowerEnum.HOSTILITY;
		private var warningValue:int=10;
		private var attackValue:int=20;
		private var defValue:int=10;
		private var csCpd:Vector.<Castle>=new Vector.<Castle>();
		public function Wisdom(scene:Scene)
		{
			_scene = scene;
		
		}
		
		public function reset(scene:Scene):void{
			_scene = scene;
			csCpd=new Vector.<Castle>();
		}
		
		private function getWeakCastle(cs:Vector.<Castle>):Castle{//最弱敌方。
			var min:Castle;//=cs[0];
			for (var i:int = 0; i < cs.length; i++) 
			{
				 if(this._power==cs[i]._power) continue;
				 if(min==null){
				     min = cs[i];
					 continue;
				 }
			     if( cs[i].antCount<min.antCount){
				    min = cs[i];
				 }	 
			}
			
			return min;
		}
		public function dispose():void{
		
		}
		/**
		 *  
		 * @param next
		 * @param targetCastle
		 * @param securityLevel 最小安全等级
		 * @return 
		 * 
		 */
		private function emerReinforcements(next:Vector.<Castle>,targetCastle:Castle,securityLevel:int):Vector.<Castle>{// 紧急
			for (var i:int = 0; i < next.length; i++) 
			{
				if(next[i]._power!=_power) continue;
				if(next[i]._securityLevel<=targetCastle._securityLevel) continue;
				if(next[i]._securityLevel<securityLevel) continue;
				if(next[i]._creating) continue;
				next[i].createAntTo(targetCastle);
				trace("紧急出兵");
			}
			
			return null;
		}
		private function transfer(castle:Castle):void //调动
		{
			var cs:Vector.<Castle> = castle._nexts;
			var min:Castle=cs[0];
			for (var i:int = 0; i < cs.length; i++) 
			{
				
				if(cs[i].antCount<min.antCount){
					min = cs[i];
				}
			}
			
			if(castle._securityLevel>min._securityLevel){
				castle.createAntTo(min);
				trace("调动");
			}else if(castle._securityLevel==min._securityLevel && castle.antCount-min.antCount>5){
				castle.createAntTo(min);
			}
		}
		private function attack(form:Castle,target:Castle):void{
			form.createAntTo(target);
		}
		public function analysis(at:Vector.<Castle>):void{
			at.sort(function(a:Castle,b:Castle):int{return a._securityLevel<b._securityLevel?-1:1});
			
			var tempCad:Castle;
			var temp:Vector.<Castle>;
			while(at.length>0){
			    tempCad = at.pop();
				if(tempCad._securityLevel==1){
					emerReinforcements(tempCad._nexts,tempCad,2);
				}else if(tempCad._securityLevel==2){
					emerReinforcements(tempCad._nexts,tempCad,4);
				}else if(tempCad._securityLevel==3){
					emerReinforcements(tempCad._nexts,tempCad,6);
				}else if(tempCad._securityLevel==4){
					emerReinforcements(tempCad._nexts,tempCad,6);
					var target:Castle = getWeakCastle(tempCad._nexts);
					var rec:int = tempCad.antCount - target.antCount
					if(rec>5 && tempCad.getAttackEnemyCount()==1){//大于最弱的地方5人以上，并且只有一个相邻的地方
						attack(tempCad,target);//攻击最弱
					}else if(tempCad.antCount / target.antCount>1.3){//大于最弱的地方1.5倍人以上
						attack(tempCad,target);//攻击最弱
					}else {
						tempCad.stopAction();
					}
				}else if(tempCad._securityLevel==5){
					//tempCad.stop();
				}else if(tempCad._securityLevel==6){
					if(!tempCad._creating){
						transfer(tempCad);
						//tempCad.createAntTo();
					}
				}
			}
		}
		

		public function updata():void{
		   var cs:Vector.<Castle>=_scene._castle;
		   var temp:int=100;
		   var level:int;
		   for (var i:int = 0; i < cs.length; i++) 
		   {
			   if(cs[i]._power!=_power) continue;
			   temp=100;
			   temp=cs[i].getEnemyCount();
			   level=0;
			   if(cs[i]._isAttack){//遭攻击
			       if(cs[i].antCount<warningValue){
					   level=1;
				   }else if(cs[i].antCount<defValue){
					   level=2; 
				   }
			   }else if(temp>0){//有邻敌
				   if(cs[i].antCount<warningValue){
					   level=3;
				   } else if(cs[i].antCount>=attackValue){
					   level=4;  
				   }else{
				       level=5
				   }
				   
			   }else{//安全
				  level=6;   
			   }
//			   if(level>0){
//			  	   trace(cs[i].flagName,level)
//			   }
			  
			   cs[i].setSecurityLevel(level);
			   csCpd.push(cs[i]);
		   }
		   analysis(csCpd);
		}
		
	}
}